import {useState, useEffect} from 'react';

const request = (url:string, params?: Object):Promise<any> => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve({
                data: Math.random(),
                code: 200
            })
          }, 500)
    })
}

const useRequest = (url:string, params?: Object):any => {

    const [data, setData] = useState(null);
    const [res, setRes] = useState(null);

    useEffect(() => {
        const http = async() => {
            let res = await request(url, params);
            setData(res.data);
            setRes(res);
        }
        http();
    //eslint-disable-next-line
    }, [])

    const reload = () => {
        // setData(null)
        request(url, params).then(res => {
            setData(res.data)
            setRes(res);
        })
    }

    return [data,reload,res]
}

export default useRequest
  